package com.example.agboxdemo.controller;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import com.example.agboxdemo.entity.vo.ResultVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author hnn
 * @version 1.0
 * @date 2025-07-07 15:07
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class BasicController {

    public static long count = 0;

    private final ObjectMapper objectMapper;

    private final Map<Integer, String> getGisTypeMap = MapUtil.of(
            Pair.of(1, "WGS84"),
            Pair.of(2, "CGCS2000"),
            Pair.of(3, "BD09"),
            Pair.of(4, "GCJ02"),
            Pair.of(5, "西安80"),
            Pair.of(6, "北京54"),
            Pair.of(7, "其它")
    );

    @PostMapping("/basic")
    public ResultVO work(@RequestParam("key") String key, @RequestParam("json") String json) {
        log.info("recv key:{},json:{}", key, json);
        ObjectNode on = objectMapper.createObjectNode();
        try {
            JsonNode jsonNode = objectMapper.readTree(json);
            String method = jsonNode.get("method").asText();
            JsonNode params = jsonNode.get("params");
            switch (method) {
                case "getGisType":
                    log.info("getGisType params:{}", params);
                    ArrayNode an = objectMapper.createArrayNode();
                    getGisTypeMap.forEach((k, v) -> {
                        an.addObject()
                                .put("code", k)
                                .put("name", v);
                    });
                    on.set("gisType", an);
                    break;
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

//        on.put("datetime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return new ResultVO<>(on);
    }
}
